package com.jinhe.tss.portal;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.jinhe.tss.cms.TxSupportTest4CMS;
import com.jinhe.tss.component.TxTestSupport;
import com.jinhe.tss.core.sso.context.Context;
import com.jinhe.tss.core.util.FileHelper;
import com.jinhe.tss.core.util.XMLDocUtil;
import com.jinhe.tss.portal.entity.Decorator;
import com.jinhe.tss.portal.entity.ElementGroup;
import com.jinhe.tss.portal.entity.Layout;
import com.jinhe.tss.portal.service.IElementService;

/**
 * Junit Test 类里执行构造函数的时候无事务，即构造函数不在单元测试方法的事物边界内。
 */
@ContextConfiguration(
        locations={
          "classpath:spring/portal-applicationContext.xml"
        } 
      )
@TransactionConfiguration(defaultRollback = false) // 不自动回滚，否则后续的test中没有初始化的数据
public abstract class TxSupportTest4Portal extends TxSupportTest4CMS { 
 
    @Autowired protected IElementService elementService;
    
    protected void displayPersistenceXML() {
        FileHelper.renameFile("META-INF/persistence.xml", "persistence.xml_portal");
    }
    
    /**
     * 初始化数据库脚本
     */
    protected void initSQL() {
    	super.initSQL();
    	TxTestSupport.excuteSQL(sqlpath + "/portal");
    }
    
    /**
     * 初始化Portal 默认的修饰器，布局器
     */
    protected void init() {
        super.init();
        
        // 门户浏览时，freemarker解析时需要用到request里的参数
        Context.initRequestContext(new MockHttpServletRequest()); 
        
        /* 初始化默认的修饰器，布局器 */
        initializeDefaultElement();
    }
 
    protected Document getApplicationPermissionConfig() {
    	return XMLDocUtil.createDocByAbsolutePath(sqlpath + "/tss-application-config.xml");
    }
    
    protected static ElementGroup defaultLayoutGroup;
    protected static Layout defaultLayout;
    protected static Long defaultLayoutId;
    protected static ElementGroup defaultDecoratorGroup;
    protected static Decorator defaultDecorator;
    protected static Long defaultDecoratorId;
    
    /**
     * 初始化默认的修饰器，布局器
     */
    public void initializeDefaultElement(){
        defaultLayoutGroup = new ElementGroup();
        defaultLayoutGroup.setName("默认布局器组");
        defaultLayoutGroup.setType(ElementGroup.LAYOUT_TYPE);
        defaultLayoutGroup.setParentId(PortalConstants.ROOT_ID);   
        defaultLayoutGroup = elementService.saveGroup(defaultLayoutGroup);
        
        defaultLayout = new Layout();
        defaultLayout.setIsDefault(PortalConstants.TRUE);
        defaultLayout.setGroupId(defaultLayoutGroup.getId());   
        Document document = XMLDocUtil.createDoc("template/initialize/defaultLayout.xml");
        Element propertyElement = document.getRootElement().element("property");
        String layoutName = propertyElement.elementText("name");
        defaultLayout.setName(layoutName);
        defaultLayout.setPortNumber(new Integer(propertyElement.elementText("portNumber")));
        defaultLayout.setDefinition(document.asXML());
        elementService.saveElement(defaultLayout);
        defaultLayoutId = defaultLayout.getId();
        
        defaultDecoratorGroup = new ElementGroup();
        defaultDecoratorGroup.setName("默认修饰器组");
        defaultDecoratorGroup.setType(ElementGroup.DECORATOR_TYPE);
        defaultDecoratorGroup.setParentId(PortalConstants.ROOT_ID);  
        defaultDecoratorGroup = elementService.saveGroup(defaultDecoratorGroup);
        
        defaultDecorator = new Decorator();
        defaultDecorator.setIsDefault(PortalConstants.TRUE);
        defaultDecorator.setGroupId(defaultDecoratorGroup.getId());
        
        document = XMLDocUtil.createDoc("template/initialize/defaultDecorator.xml");
        propertyElement = document.getRootElement().element("property");
        String decoratorName = propertyElement.elementText("name");
        defaultDecorator.setName(decoratorName);
        defaultDecorator.setDefinition(document.asXML());
        elementService.saveElement(defaultDecorator);
        defaultDecoratorId = defaultDecorator.getId();
    }
}
